### Copyright (C) 2010 Desmond Carter
###
### This file is part of Ar.
###
### Ar is free software: you can redistribute it and/or modify it under the
### terms of the GNU General Public License as published by the Free Software
### Foundation, either version 3 of the License, or (at your option) any later
### version.
###
### Ar is distributed in the hope that it will be useful, but WITHOUT ANY
### WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
### FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
### details.
###
### You should have received a copy of the GNU General Public License along
### with Ar. If not, see <http://www.gnu.org/licenses/>.

## This file contains the class for the main board.


from ar.locals import *
import ar, ar.drawtools


class ArBoard(object):
    '''Class for representing boards. The main one should be called board.
    pieces should be a dict indexed by coordinates.'''

    def __init__(self, axes=[], pieces=[]):
        self.axes = sorted(axes)

        # This might not be necessary any more, because the "contains" thing is
        # pretty helpful. However, I think it's the only way to have lookup of
        # pieces by coordinates.
        self.pieces = pieces

        # I've used the mathematical term "basis" because I'm already using
        # "dimension" for way too much other stuff.
        self.basis = len(self.axes) - 1

        # This is currently not kept track of.
        self.visible_list = []

        # This is the length of the coordinate sequences required to locate a
        # point on the board. It is the same as basis except it is not zero-
        # based.
        self.coord_length = self.basis + 1

        # Check for axes that have bad vectorforms--board will always have
        # these but the rest shouldn't.
        for vector in map(lambda x: x.vectorform, self.axes):
            vector.extend([0] * (self.coord_length - len(vector)))

        # Make sure it is the parent of each of its axes.
        for axis in self.axes:
            if axis.parent is not self:
                axis.parent = self

    def contains(self, point):
        for axis in self.axes:
            if not axis.contains(point): # so it short-circuits
                return False
        return True

    def draw(self, origin=ar.origin, drawn_dimensions=[0, 1, 2],
             screen=ar.screen):
        '''Draw up to three dimensions of the board and all pieces contained in
        that section.'''

        # I can't figure out another way to get this to work. Cyclic
        # dependencies confuse me. I guess they /are/ confusing.
        from ar.classes import BoardSegment

        projection = BoardSegment(self, drawn_dimensions)

        num_axes = projection.coord_length
        for i, axis in enumerate(projection.axes):
            axis.draw(screen=screen)
            axis.tickmarks([projection.axes[(i + 1) % num_axes],
                            projection.axes[(i + 2) % num_axes]],
                           DIMENSION_COLORS[i % 3], screen=screen)

        map(lambda x: None, projection.pieces) # This is obviously a dummy
